import { getToken } from '@/utils/auth.js'
import settings from '@/settings'

import dataApi from '@/api/knowledge/knowledgeContent.js'

const tokenName = settings.tokenName

const downloadAction = process.env.VUE_APP_BASE_API + '/sys/common/download'

export default {
  methods: {
    downloadOssFile(ossFileId, fileName) {
      const token = getToken()
      const action = `${downloadAction}/${ossFileId}?${tokenName}=${token}&fileName=${encodeURI(fileName)}`
      window.open(action)
    },
    downloadKnowledgeFile(ossFileId, fileName, knowledgeId) {
      const token = getToken()
      const action = `${downloadAction}/${ossFileId}?${tokenName}=${token}&fileName=${encodeURI(fileName)}`
      window.open(action)
      dataApi.downloadCount(knowledgeId)
    },
    exportReportExcel(name, data) {
      const token = getToken()
      const action = process.env.VUE_APP_BASE_API + `/statistics/report-export/${name}?${tokenName}=${token}&params=` + encodeURI(JSON.stringify(data))
      window.open(action)
    },
    downloadByUrl(url) {
      if (!url) {
        return
      }

      if (!url.startsWith('http://') && !url.startsWith('https://')) {
        url = process.env.VUE_APP_BASE_API + url
      }

      const token = getToken()
      const action = `${url}?${tokenName}=${token}`
      window.open(action)
    },
    getDownloadUrl(ossFileId) {
      const token = getToken()
      return `${downloadAction}/${ossFileId}?${tokenName}=${token}`
    }
  }
}
